function changeStrTime(time) {
    if (null === time || '' === time || typeof time === "undefined" || time.length < 12 || time === 0) {
        return "";
    } else {
        if (time.indexOf('-') > 0) {
            return time.substring(0, 16);
        } else {
            if (time.length <= 13) {
                return formatTime(Number(time));
            } else {
                return time.substring(0, 4) + "-" + time.substring(4, 6) + "-" + time.substring(6, 8) + " " + time.substring(8, 10) + ":" + time.substring(10, 12);
            }
        }
    }
}

// 时间戳转换函数
function formatTime(timestamp) {
    // 验证输入
    if (null == timestamp) return "暂无时间";
    if (isNaN(timestamp)) return "暂无时间";

    // 检查时间戳是否在有效范围内
    if (timestamp < -8640000000000000 || timestamp > 8640000000000000) {
        return "暂无时间";
    }

    // 创建Date对象
    const date = new Date(timestamp);

    // 验证Date对象是否有效
    if (isNaN(date.getTime())) {
        return "暂无时间";
    }

    // 获取各个时间组件
    let year = date.getFullYear(), month = date.getMonth() + 1, //月份是从0开始的
        day = date.getDate(), hour = date.getHours(), min = date.getMinutes(), sec = date.getSeconds();
    return year + '-' + (month < 10 ? '0' + month : month) + '-' + (day < 10 ? '0' + day : day) + ' ' + (hour < 10 ? '0' + hour : hour) + ':' + (min < 10 ? '0' + min : min) + ':' + (sec < 10 ? '0' + sec : sec);
}

// 显示加载动画
function showLoading() {
    const overlay = document.createElement('div');
    overlay.className = 'loading-overlay';
    overlay.innerHTML = `<div class="loading-spinner"></div><div class="loading-text">数据加载中...</div>`;
    document.body.appendChild(overlay);
    overlay.style.display = 'flex';
}

// 隐藏加载动画
function hideLoading() {
    const overlay = document.querySelector('.loading-overlay');

    if (overlay) {
        overlay.style.display = 'none';
        overlay.remove();
    }
}

// 创建表格
function createTable(tableId, title, data) {
    const table = document.getElementById(tableId);
    table.innerHTML = '';

    // 创建表头
    const thead = document.createElement('thead');
    const headerRow = document.createElement('tr');
    title.forEach(text => {
        const th = document.createElement('th');
        th.textContent = text;
        th.style.whiteSpace = 'normal';
        headerRow.appendChild(th);
    });
    thead.appendChild(headerRow);
    table.appendChild(thead);

    // 创建表格体
    const tbody = document.createElement('tbody');
    data.forEach(rowData => {
        const tr = document.createElement('tr');
        rowData.forEach((cell, index) => {
            const td = document.createElement('td');
            let tdWidth = '100px';

            td.setAttribute("data-index", index);

            if (typeof cell !== "undefined") {
                td.textContent = cell;
            } else {
                td.textContent = '-';
            }

            if (index === 0) {
                td.style.fontWeight = 'bold';
                td.style.width = '100px';
            } else {
                tdWidth = ((td.textContent.length * 10) + 'px');
                td.style.width = tdWidth;
            }

            tr.appendChild(td);
        });
        tbody.appendChild(tr);
    });
    table.appendChild(tbody);
}

// 初始化选项卡
function initTabs() {
    document.querySelectorAll('.tab').forEach(tab => {
        tab.addEventListener('click', function () {
            // 移除所有激活状态
            document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));

            // 设置当前激活状态
            this.classList.add('active');
        });
    });
}

function changeTab(type) {
    $(".tab").each(function () {
        $(this).removeClass('active');
    });

    $('#tab' + type).addClass('active');

    sessionStorage.setItem('selectDataType', type);
}

function goBack(num) {
    if (Number(num) > 0) {
        switch (Number(num)) {
            case 1:
            default:
                window.location.href = 'wx/project/index';
                break;
            case 2:
                window.location.href = 'wx/project/main';
                break;
            case 3:
                window.location.href = 'wx/project/device';
                break;
        }
    } else {
        window.location.href = 'wx/project/index';
    }
}